package com.yd.mvc.baseController.util;



import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

/**
 * 读取配置文件的工具类
 * 
 * @version 0.1
 * @date 2016.7.25
 * 
 */
public class LoadPropertyUrl {
	private static Properties properties;
	private static String path;
	
	/**图片存放地址**/
	public static final String IMG_TEMPPATH = "img.tempPath";
	/**视频存放地址**/
	public static final String VIDEO_TEMPPATH = "video.tempPath";
	/**文件存放地址**/
	public static final String FILE_TEMPPATH = "file.tempPath";
	
	static{
		try{
			path = Thread.currentThread().getContextClassLoader().getResource("").getPath()+File.separator+"application.properties";
			path = URLDecoder.decode(path,"utf-8");//%20转换空格解决
			properties = new Properties();
			properties.load(new FileInputStream(new File(path)));
			if(properties==null){
				throw new Exception("配置文件内容为空");
			}
		}catch(Exception e){
		}
	}
	

	public static String getValue(String key) {
		return properties.getProperty(key);
	}

	//获取static文件夹路径
	public static String getPath() {
		try {
			path=Thread.currentThread().getContextClassLoader().getResource("").getPath();
			path = URLDecoder.decode(path,"utf-8"); //%20转换空格解决
			path=path.replace('/', '\\'); // 将/换成\  
	        path=path.replace("file:", ""); //去掉file:  
	        path=path.replace("WEB-INF\\classes\\", "WEB-INF\\"); //去掉class\
	        path=path.substring(1); //去掉第一个\,如 \D:\JavaWeb...  
	        path+="static\\"; 
		} catch (UnsupportedEncodingException e) {
		}
		return path;
	}
	//获取指定文件夹路径
	public static String getPath(String folder) {
		try {
			path=Thread.currentThread().getContextClassLoader().getResource("").getPath();
			path = URLDecoder.decode(path,"utf-8"); //%20转换空格解决
			path=path.replace('/', '\\'); // 将/换成\  
	        path=path.replace("file:", ""); //去掉file:  
	        path=path.replace("WEB-INF\\classes\\", ""); //去掉class\  
	        path=path.substring(1); //去掉第一个\,如 \D:\JavaWeb...  
	        path+="static\\"+folder; 
		} catch (UnsupportedEncodingException e) {
		}
		return path;
	}

	public static void setPath(String path) {
		LoadPropertyUrl.path = path;
	}
}
